package cn.kk.base.model;

import cn.kk.utils.BeanHelper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * 服务层基类
 * @author kinko
 */
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> {

    /**
     * 转化成mybatis plus中的Page
     * @param query 查询条件
     * @return IPage
     */
    public static <T> Page<T> getPage(BaseQuery query) {
        return new Page<>(query.getPage(), query.getSize());
    }

    /**
     * entityPage 转 VOPage
     * @param page 基础分页
     * @return IPage
     */
    public static <T> Page<T> toVOPage(Page<?> page, Class<T> c) {
        Page<T> voPage = BeanHelper.copyProperties(page, Page.class);
        if (voPage == null) {
            return null;
        }
        List<T> voList = BeanHelper.copyWithCollection(page.getRecords(), c);
        voPage.setRecords(voList);
        return voPage;
    }
}
